package com.yyfax.ph.common.share;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareActionListener implements PlatformActionListener {
    private static final int ACTION_SINA_AUTHORIZING_SUCCESS = 0x0001;
    private static final int ACTION_SINA_AUTHORIZING_ERROR = 0x0002;
    private static final int ACTION_SINA_AUTHORIZING_CANCEL = 0x0003;

    private static final int ACTION_SINA_SHARE_SUCCESS = 0x0004;
    private static final int ACTION_SINA_SHARE_ERROR = 0x0005;
    private static final int ACTION_SINA_SHARE_CANCEL = 0x0006;

    private static final int ACTION_WECHAT_MOMENTS_SHARE_SUCCESS = 0x0007;
    private static final int ACTION_WECHAT_MOMENTS_SHARE_CANCEL = 0x0008;
    private static final int ACTION_WECHAT_MOMENTS_SHARE_ERROR = 0x0009;

    private static final int ACTION_QZONE_SHARE_SUCCESS = 0x00010;
    private static final int ACTION_QZONE_SHARE_CANCEL = 0x00011;
    private static final int ACTION_QZONE_SHARE_ERROR = 0x00012;

    private static final int ACTION_QQ_FRIEND_SHARE_SUCCESS = 0x00013;
    private static final int ACTION_QQ_FRIEND_SHARE_CANCEL = 0x00014;
    private static final int ACTION_QQ_FRIEND_SHARE_ERROR = 0x00015;

    private static final int ACTION_WE_CHAT_FRIEND_SHARE_SUCCESS = 0x00016;
    private static final int ACTION_WE_CHAT_FRIEND_SHARE_CANCEL = 0x00017;
    private static final int ACTION_WE_CHAT_FRIEND_SHARE_ERROR = 0x00018;

	private static final Map<Integer, String> mMESSAGE_MAP = new HashMap<Integer, String>(){{
		put(ACTION_SINA_AUTHORIZING_SUCCESS, "");
		put(ACTION_SINA_AUTHORIZING_ERROR, "新浪微博授权失败");
		put(ACTION_SINA_SHARE_SUCCESS, "分享到新浪微博成功");
		put(ACTION_SINA_SHARE_CANCEL, "分享到新浪微博操作已取消");
		put(ACTION_SINA_SHARE_ERROR, "分享到新浪微博失败");
		put(ACTION_WECHAT_MOMENTS_SHARE_SUCCESS, "分享到朋友圈成功");
		put(ACTION_WECHAT_MOMENTS_SHARE_CANCEL, "分享到朋友圈操作已取消");
		put(ACTION_WECHAT_MOMENTS_SHARE_ERROR, "分享到朋友圈失败");
		put(ACTION_QZONE_SHARE_SUCCESS, "分享到QQ空间成功");
		put(ACTION_QZONE_SHARE_CANCEL, "分享到QQ空间操作已取消");
		put(ACTION_QZONE_SHARE_ERROR, "分享到QQ空间失败");
		put(ACTION_QQ_FRIEND_SHARE_SUCCESS, "分享到QQ好友成功");
		put(ACTION_QQ_FRIEND_SHARE_CANCEL, "分享到QQ好友操作已取消");
		put(ACTION_QQ_FRIEND_SHARE_ERROR, "分享到QQ好友失败");
		put(ACTION_WE_CHAT_FRIEND_SHARE_SUCCESS, "分享到微信好友成功");
		put(ACTION_WE_CHAT_FRIEND_SHARE_CANCEL, "分享到微信好友操作已取消");
		put(ACTION_WE_CHAT_FRIEND_SHARE_ERROR, "分享到微信好友失败");
	}};

	ShareActionListener(Context ctx, String title, String content, String url) {
		mShareHandler = new ShareHandler(ctx, title, content, url);
	}

	void removeDefaultPlatformActionListener() {
		mShareHandler.removeCallbacksAndMessages(null);
	}

    private static ShareHandler mShareHandler = null;

	private static class ShareHandler extends Handler {
		private final WeakReference<Context> mContext;
		private String title, content, url;

		ShareHandler(Context ctx, String title, String content, String url) {
			mContext = new WeakReference<>(ctx);
			this.title = title;
			this.content = content;
			this.url = url;
		}

		@Override
		public void handleMessage(Message msg) {
			Context ctx = mContext.get();
			if (null == ctx) {
				return;
			}

			int what = msg.what;
			String message = mMESSAGE_MAP.get(what);
			switch (what) {
				case ACTION_SINA_AUTHORIZING_SUCCESS:
					ShareAgent.shareWeiBo(ctx, title, content, url, -1);
					break;
				default:
					if (!TextUtils.isEmpty(message))
						Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
					break;
			}
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public void setContent(String content) {
			this.content = content;
		}
	}

    @Override
    public void onCancel(final Platform platform, final int action) {
        String platformName = platform.getName();
        int what = -1;

        if (SinaWeibo.NAME.equals(platformName)) {
            if (action == SinaWeibo.ACTION_AUTHORIZING) {
                what = ACTION_SINA_AUTHORIZING_CANCEL;
            } else if (action == SinaWeibo.ACTION_SHARE) {
                what = ACTION_SINA_SHARE_CANCEL;
            }
        } else if (WechatMoments.NAME.equals(platformName)) {
            if (action == WechatMoments.ACTION_SHARE) {
                what = ACTION_WECHAT_MOMENTS_SHARE_CANCEL;
            }
        } else if (QZone.NAME.equals(platformName)) {
            if (action == QZone.ACTION_SHARE) {
                what = ACTION_QZONE_SHARE_CANCEL;
            }
        } else if (QQ.NAME.equals(platformName)) {
            if (action == QQ.ACTION_SHARE) {
                what = ACTION_QQ_FRIEND_SHARE_CANCEL;
            }
        } else if (Wechat.NAME.equals(platformName)) {
            if (action == Wechat.ACTION_SHARE) {
                what = ACTION_WE_CHAT_FRIEND_SHARE_CANCEL;
            }
        }
        mShareHandler.sendEmptyMessage(what);
    }

    @Override
    public void onComplete(final Platform platform, final int action, final HashMap<String, Object> res) {
        String platformName = platform.getName();
        int what = -1;

        if (SinaWeibo.NAME.equals(platformName)) {
            if (action == SinaWeibo.ACTION_AUTHORIZING) {
                what = ACTION_SINA_AUTHORIZING_SUCCESS;
            } else if (action == SinaWeibo.ACTION_SHARE) {
                what = ACTION_SINA_SHARE_SUCCESS;
            }
        } else if (WechatMoments.NAME.equals(platformName)) {
            if (action == WechatMoments.ACTION_SHARE) {
                what = ACTION_WECHAT_MOMENTS_SHARE_SUCCESS;
            }
        } else if (QZone.NAME.equals(platformName)) {
            if (action == QZone.ACTION_SHARE) {
                what = ACTION_QZONE_SHARE_SUCCESS;
            }
        } else if (QQ.NAME.equals(platformName)) {
            if (action == QQ.ACTION_SHARE) {
                what = ACTION_QQ_FRIEND_SHARE_SUCCESS;
            }
        } else if (Wechat.NAME.equals(platformName)) {
            if (action == Wechat.ACTION_SHARE) {
                what = ACTION_WE_CHAT_FRIEND_SHARE_SUCCESS;
            }
        }
        mShareHandler.sendEmptyMessage(what);
    }

    @Override
    public void onError(final Platform platform, final int action, final Throwable t) {
        String platformName = platform.getName();
        int what = -1;

        if (SinaWeibo.NAME.equals(platformName)) {
            if (action == SinaWeibo.ACTION_AUTHORIZING) {
                what = ACTION_SINA_AUTHORIZING_ERROR;
            } else if (action == SinaWeibo.ACTION_SHARE) {
                what = ACTION_SINA_SHARE_ERROR;
            }
        } else if (WechatMoments.NAME.equals(platformName)) {
            if (action == WechatMoments.ACTION_SHARE) {
                what = ACTION_WECHAT_MOMENTS_SHARE_ERROR;
            }
        } else if (QZone.NAME.equals(platformName)) {
            if (action == QZone.ACTION_SHARE) {
                what = ACTION_QZONE_SHARE_ERROR;
            }
        } else if (QQ.NAME.equals(platformName)) {
            if (action == QQ.ACTION_SHARE) {
                what = ACTION_QQ_FRIEND_SHARE_ERROR;
            }
        } else if (Wechat.NAME.equals(platformName)) {
            if (action == Wechat.ACTION_SHARE) {
                what = ACTION_WE_CHAT_FRIEND_SHARE_ERROR;
            }
        }
        mShareHandler.sendEmptyMessage(what);
    }

	public void resetTitleAndContent(String title, String content){
		mShareHandler.setContent(content);
		mShareHandler.setTitle(title);
	}
}
